clear
clear matrix
clear mata
program drop _all
set more off
set emptycells drop
set maxvar 100000
tempfile main

cap log close
log using Table12actual,replace

cd "<directory>"

*The code in this .do file reads in the dataset created in Data Cleaning Step 2.do and runs the regressions for Table 12;
*Before the estimation commands are run, the quarter of the rule change is dropped, and additional fixed effects are created;
*The "_m" suffixes in the outreg commands stipulate which DPO window the model corresponds to:;
*	_m1 = DPO[-4,-7];
*	_m2 = DPO[-5,-8];
*	_m3 = DPO[-6,-9];
*	_m4 = DPO[-7,-10];

/*********************************************************************************
*Note: The following block of code appears before every regression. This code identifies and drops observations that Stata will automatically drop in the course of the probit estimation. This code is optional and all the regressions can be run without it. However, using this code speeds up the estimation significantly, because Stata does not have to determine whether to drop these obervations as it goes through the estimation;
*	tempfile msayq
*	keep if !missing(transfer, post, dspp0_5t8, post_dspp0_5t8, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
*	keep if dcountss_2==1
*	keep if yearmonth>=572 & yearmonth<=620
*	collapse (mean) transfer,by(msayq)
*	gen indic=1 if transfer==0
*	keep msayq indic
*	sa `msayq',replace
*	u `main',clear
*	merge m:1 msayq using `msayq'
*	drop if _merge==2
*	drop _merge
***********************************************************************************/



u final_dataset,clear
drop if distyq==198
egen dealid=group(dosname)
egen ss_ms=group(ssid msid)
sa `main',replace


u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_5t8, post_dspp0_5t8, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, originatorid, origyq, msayq, dealid)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge


probit transfer dspp0_5t8 post_dspp0_5t8 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.ss_ms if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel replace stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)




u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_4t7, post_dspp0_4t7, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_4t7 post_dspp0_4t7,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge

probit transfer dspp0_4t7 post_dspp0_4t7 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.ss_ms if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)





u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_6t9, post_dspp0_6t9, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_6t9 post_dspp0_6t9,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge


probit transfer dspp0_6t9 post_dspp0_6t9 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.ss_ms if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)




u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_7t10, post_dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_7t10 post_dspp0_7t10,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge

probit transfer dspp0_7t10 post_dspp0_7t10 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.ss_ms if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)







u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_5t8, post_dspp0_5t8, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, originatorid, origyq, msayq, dealid)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge


probit transfer dspp0_5t8 post_dspp0_5t8 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.dealid if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)






u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_4t7, post_dspp0_4t7, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_4t7 post_dspp0_4t7,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge

probit transfer dspp0_4t7 post_dspp0_4t7 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.dealid if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)




u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_6t9, post_dspp0_6t9, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_6t9 post_dspp0_6t9,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge


probit transfer dspp0_6t9 post_dspp0_6t9 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.dealid if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)




u `main',clear
tempfile msayq
keep if !missing(transfer, post, dspp0_7t10, post_dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_7t10 post_dspp0_7t10,by(msayq)
gen indic=1 if transfer==0
keep msayq indic
sa `msayq',replace
u `main',clear
merge m:1 msayq using `msayq'
drop if _merge==2
drop _merge

 

probit transfer dspp0_7t10 post_dspp0_7t10 orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.originatorid i.origyq i.msayq i.dealid if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using Table12, auto(2) excel append stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, N, PropType FE, Y, DealType FE, N, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, Deal FE, Y, SE Clust by loan, Y)